<?php

namespace app\admin\controller;

use think\Cache;
use think\Controller;
use app\admin\controller\Index;
define("TOKEN", "wangkang");
class Message extends Controller
{
//    public function valid(){
//        //获取随机字符串
//        $echoStr = input("echostr");Cache::set('wk',$echoStr);exit();
//        if($echoStr){
//            // 验证接口的有效性，由于接口有效性的验证必定会传递echostr 参数
//            if($this ->checkSignature()){
//                Cache::set('oo','123');exit();
//                echo $echoStr;
//                exit;
//            }
//        }else{
//            $this->responseMsg();
//        }
//    }


    public function index(){
        if (isset($_GET['echostr'])) {
            $this->valid();
        }else{
            $this->responseMsg();
        }
    }

    public function valid()
    {
        $echoStr = $_GET["echostr"];
        //valid signature , option
        if(!$this->checkSignature()){
            echo $echoStr;
            exit;
        }else{
            $this->receiveMsg();
        }

    }

    public function receiveMsg()
    {
        $postStr = file_get_contents('php://input','r');

        Cache::set('cannel',$postStr);
        if (!empty($postStr)){
            Cache::set('data',$postStr);
            libxml_disable_entity_loader(true); //改为这句
            //将xml转化为数组
            $postArray=json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            //接收方帐号（收到的OpenID）
            $FromUserName=$postArray['FromUserName'];
            //开发者微信号
            $ToUserName=$postArray['ToUserName'];
            //创建时间
            $time=time();
            $keyword=trim($postArray['Content']);
            if (isset($postArray['Event'])&&$postArray['Event']=='subscribe'){
                Cache::set('kkkk',$postArray);
                $text='<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[欢迎!]]></Content>
                </xml>';
                $resultStr = sprintf($text, $FromUserName,$ToUserName,  $time);
                echo $resultStr;
            }
            Cache::set('kkkk',$postArray);

//            $text='<xml>
//                <ToUserName><![CDATA[%s]]></ToUserName>
//                <FromUserName><![CDATA[%s]]></FromUserName>
//                <CreateTime>%s</CreateTime>
//                <MsgType><![CDATA[%s]]></MsgType>
//                <MediaId><![CDATA[%s]]></MediaId>
//                </xml>';
//            $img='<xml>
//                <ToUserName><![CDATA[%s]]></ToUserName>
//                <FromUserName><![CDATA[%s]]></FromUserName>
//                <CreateTime>%s</CreateTime>
//                <MsgType><![CDATA[%s]]></MsgType>
//                <Image>
//                <MediaId><![CDATA[%s]]></MediaId>
//                </Image>
//                </xml>';
            $new='<xml>
                   <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <ArticleCount>2</ArticleCount>
                    <Articles>
                    <item>
                    <Title><![CDATA[著名旅游景点]]></Title>
                    <Description><![CDATA[法国香波城堡]]></Description>
                    <PicUrl><![CDATA[http://t2.hddhhn.com/uploads/tu/201809/9999/8e5bcee8b6.jpg]]></PicUrl>
                    <Url><![CDATA[www.baidu.com]]></Url>
                    </item>
                    <item><Title><![CDATA[著名旅游景点]]></Title>
                    <Description><![CDATA[法国香波城堡]]></Description>
                    <PicUrl><![CDATA[http://t2.hddhhn.com/uploads/tu/201809/9999/8e5bcee8b6.jpg]]></PicUrl>
                    <Url><![CDATA[www.baidu.com]]></Url>
                    </item>
                    </Articles>
                    </xml>';
            if(!empty($keyword))
            {
                $msgType = "news";
//                $contentStr = "PYlPvgtavA--Nlc0RinZLCVjIMpnsfK7bpbF8lgc8YdSHGbrqIeFuYPj4BSPrxCL";
                $resultStr = sprintf($new, $FromUserName,$ToUserName,  $time, $msgType);
                echo $resultStr;
            }else{
                echo "Input something...";
            }

        }else{
            echo "";
            exit;
        }



    }



    private function checkSignature()
    {
        // you must define TOKEN by yourself
        if (!defined("wangkang")) {
            throw new Exception('TOKEN is not defined!');
        }

        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        // 1）将token、timestamp、nonce三个参数进行字典序排序
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        sort($tmpArr, SORT_STRING);
        // 2）将三个参数字符串拼接成一个字符串进行sha1加密
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        // 3）开发者获得加密后的字符串可与signature对比，标识该请求来源于微信

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }


}
